<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>  
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" href="static/easyui/themes/metro/easyui.css"/>
<link rel="stylesheet" href="static/easyui/themes/icon.css"/>
<!-- 用户详情css -->
<link rel="stylesheet"  type="text/css" href="static/css/User.css"/>

<title>用户管理</title>
 
</head>
<body>
<script type="text/javascript">
//获取页面的二级功能id
var jq = top.jQuery;
var funId=jq("#tt").tabs("getSelected").panel('options').id;
console.log(funId);
//初始化三级的功能按钮
var btnss = function(value,row,index){
	var str="";
	$.ajax({
		url:'init-thirdfuns',
		type:'post',
		data:{
			funId:funId//参数为2级的功能id
		},
		async:false,//是否异步刷新
		success:function(data){
			for (var i = 0; i < data.length; i++) {
				str += "<input type='button' onClick='"+data[i]['function_url']+"("+row.user_id+")' value='"+data[i]['function_name']+"'/>";
			}
		}
	})
return str;
} 

$(function(){
	//用户表格
	  $('#user').datagrid({
	    url:'user-list',
	    pagination:true,
	    pageNumber:1,
	    pageSize:10,
	    pageList:[10,9,8],
	    columns:[[  
	        {field:'user_name',title:'姓名',width:180},    
	        {field:'user_sex',title:'性别',width:80,},
	        {field:'role_name',title:'职位',width:180,},	     
	        {field:'section_name',title:'所属科室',width:120},
	        {field:'user_state',title:'在职状态',width:100,},	
	        {field:'User details',title:'操作',width:381,fixed:true,
	         formatter:btnss  
	        	   /* formatter:function(value,row,index){		        	 
				return  "<a class='easyui-linkbutton' onclick='lizhi("+row.user_id+")'>离职</a><a class='easyui-linkbutton' onclick='xiugaimima("+row.user_id+")'>修改密码</a><a class='easyui-linkbutton' onclick='addrole("+row.user_id+")'>添加角色</a><a class='easyui-linkbutton' onclick='userxiang("+row.user_id+")'>用户详情</a>";		         	
		          }	  */   
	        },
	    ]]       
	}); 
	
	//用户搜索
  $(".search-user").searchbox({
		searcher:function(value){
			  $('#user').datagrid({
				    url:'user-list',
				    queryParams:{
				    	username:value
				    },
				    pagination:true,
				    pageNumber:1,
				    pageSize:10,
				    pageList:[10,9,8],
				    columns:[[  
				        {field:'user_name',title:'姓名',width:180},    
				        {field:'user_sex',title:'性别',width:80,},
				        {field:'role_name',title:'职位',width:180,},	     
				        {field:'section_name',title:'所属科室',width:120},
				        {field:'user_state',title:'在职状态',width:100,},	
				        {field:'User details',title:'操作',width:381,fixed:true,
				         formatter:btnss  
				        	   /* formatter:function(value,row,index){		        	 
							return  "<a class='easyui-linkbutton' onclick='lizhi("+row.user_id+")'>离职</a><a class='easyui-linkbutton' onclick='xiugaimima("+row.user_id+")'>修改密码</a><a class='easyui-linkbutton' onclick='addrole("+row.user_id+")'>添加角色</a><a class='easyui-linkbutton' onclick='userxiang("+row.user_id+")'>用户详情</a>";		         	
					          }	  */   
				        },
				    ]]       
				}); 
		}
	});
	
	
	
}) ; 		
 	 
		
		var jq = top.jQuery;
		var funId1=jq("#tt").tabs("getSelected").panel('options').id;		
		var btnsss = function(value,row,index){
			var str="";
			$.ajax({
				url:'init-allfourFuns',
				type:'post',
				data:{
					funId:funId1//参数为2级的功能id
				},
				async:false,//是否异步刷新
				success:function(data){
					for (var i = 0; i < data.length; i++) {
						str += "<input type='button' onClick='"+data[i]['function_url']+"("+row.user_id+")' value='"+data[i]['function_name']+"'/>";
					}
				}
			})
	return str;
			
} 
 
//用户详情
function userxiang(user_id){
	//alert(user_id);
 	 $('#userxiangqing1').window('open');
				  $('#userxiangqing').datagrid({
					    url:'selectAll-userdetails',
					    queryParams:{
					    	uid:user_id
					    },					 
					    columns:[[ 
					    	{field:'user_tel',title:'电话',width:100},
					    	{field:'user_card',title:'身份证',width:160},
					    	{field:'user_address',title:'地址',width:160},
					    	{field:'user_reg_time',title:'注册时间',width:160,formatter:crtTimeFtt},
					    	{field:'User details',title:'操作',width:140,fixed:true,
								 formatter:btnsss		
					    		/*  formatter:function(value,row,index){		        	 
					    		return  "<a class='easyui-linkbutton' onclick='ckjuese("+row.user_id+")'>查看角色</a><a class='easyui-linkbutton' onclick='ckfunction("+row.user_id+")'>查看功能</a>";		         	
					    				          }	    */
								}					        	   
					    	]]  
					}); 
				  
} 
//查看用户角色
function ckjuese(user_id){
	//alert(user_id);
	 $('#select-role1').window('open');
	  $('#select-role').datagrid({
		    url:'selectAllrole',
		    queryParams:{
		    	uid:user_id
		    },					 
		    columns:[[  
		    	{field:'role_name',title:'角色',width:720}
		    ]]       
		}); 	
}



//查看用户功能
function ckfunction(user_id){
	//alert(user_id);	 
	 $('#select-function1').window('open');
	  $("#userfunction").tree({
			url:'selectAllfunction?user_id='+user_id,
			onlyLeafCheck:true
		}); 
}

	
//时间格式化
function crtTimeFtt(val, row) {
	    if (val != null) {
	            var date = new Date(val);
	            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
	        }
	}
 
	
	
//在职状态
 function lizhi(user_id){
	 //alert("用户"+user_id);
	 $.ajax({
		    url:"updatestate",
		    data:{'userId':user_id},
		    type:"post",
		    success:function(data){	
		    	 $('#user').datagrid('reload');
		    }
		});
 }
//新增用户角色
 function addrole(user_id){
	 //alert(user_id);
	 $("#userids").textbox('setValue',user_id);
	 $('#tianjiarole').window('open');
	 
	 $("#grant-tree1").tree({
			checkbox:true,
			url:'userRoleList?user_id='+user_id,
			onlyLeafCheck:true
		});
	  /* $('#chenkrole').combobox({   
		  	url:'role-list',//数据源
		  	 valueField: 'roleId',//实体类名字---主键id
		     textField: 'roleName',//需要显示在下拉框的实际内容
		     editable:false,
		     panelHeight:'auto',//高度根据内容自动定
		     onLoadSuccess: function () {  //加载完成后,设置选中第一项
                 var val = $(this).combobox("getData");
                 for (var item in val[0]) {
                     if (item == "roleId") {
                     	//初始化数据默认选中第一个
                         $(this).combobox("select", val[0][item]);
                     }
                 }
           }
	  });  */
	  
 }
//给用户添加角色
 $('#queren').click(function(){
		var nodes = $('#grant-tree1').tree('getChecked');
		var usderid = $("#userids").val();
		 //alert(usderid);
		//console.log(nodes);
		var functionIds = [];
		for (var i = 0; i < nodes.length; i++) {
			//这是三级
			functionIds.push(nodes[i].id);
		}
		//去重
		function dedupe(array){
			 return Array.from(new Set(array));
		}
		functionIds = dedupe(functionIds) //保证一级和二级在数组中不重复
		//提交到controller
		$.ajax({
			url:'adduserrole',
			type:'post',
			data:{
				userId:usderid,
				funIdss:JSON.stringify(functionIds)
			},
			//表单提交成功之后的数据
		    success:function(data){
		    	$("#tianjiarole").window("close"); 
		    } 
		});   
});



 //重置密码窗口打开
  function xiugaimima(user_id){
	 $('#updatepwda').window('open');
	 
	 //给用户编号赋值   
	 $("#userid").textbox('setValue',user_id);
	 $("#userid").next().hide();
 }
//修改密码
  $("#queding").click(function(){
	    var dd=$('#userid').val();
	    var dd2=$('#updatpwdq').val();
		 $.ajax({
			    url:"xg",
			    data:{'userid':dd ,'updatpwdq':dd2},
			    type:"post",
			    success:function(data){				    	
			    	 $('#updatepwda').window('close'); 
			    	  $('#user').datagrid('reload');
			    	  $("#updatpwdq").textbox("setValue","");
			    }
			});   
	});  
function qq(value,name){ 
	//alert(value+":"+name) 
	} 

//用户新增窗口打开alert
  function userinsert(){	
  	 $('#quanxian-adduser').window('open');
	  	 $('#keshiID').combobox({	  		
			  	url:'selcetSection',//数据源
			  	 valueField: 'sectionId',//实体类名字---主键id
			     textField: 'sectionName',//需要显示在下拉框的实际内容
			     editable:false,
			     panelHeight:'auto',//高度根据内容自动定
			     onLoadSuccess: function () {  //加载完成后,设置选中第一项
	              var val = $(this).combobox("getData");
	              for (var item in val[0]) {
	                  if (item == "sectionId") {
	                  	//初始化数据默认选中第一个
	                      $(this).combobox("select", val[0][item]);
	                  }
	              }
	        }
		  }); 
	  	 $('#roleIDs').combobox({
				  	url:'role-list',//数据源
				  	 valueField: 'roleId',//实体类名字---主键id
				     textField: 'roleName',//需要显示在下拉框的实际内容
				     editable:false,
				     panelHeight:'auto',//高度根据内容自动定
				     onLoadSuccess: function () {  //加载完成后,设置选中第一项
		              var val = $(this).combobox("getData");
		              for (var item in val[0]) {
		                  if (item == "roleId") {
		                  	//初始化数据默认选中第一个
		                      $(this).combobox("select", val[0][item]);
		                  }
		              }
		        }
			  }); 
  }
//用户新增
$("#userinserts").click(function(){
	$('#ff').form({    
	    url:'adduser',    
	    onSubmit: function(){   
	    	 var c=$('#ff').form("validate");  
	    	 return c;
	    }, 
	    //表单提交成功之后的数据
	    success:function(data){ 
	    	 $('#quanxian-adduser').window('close');
	    	  $('#user').datagrid('reload');
	    	  $('#tjdxm').textbox("setValue","");
	    	  $('#tjdmm').textbox("setValue","");
	    	  $('#tjddd').textbox("setValue","");
	    	  $('#tjdsfz').textbox("setValue","");
	    	  $('#tjdsjh').textbox("setValue","");
	    }    
	}).submit();  
	
});	

$.extend($.fn.textbox.defaults.rules, {
				    //姓名验证
					chinese: {
				        validator: function(value, param) {
				            var reg = /^[\u4e00-\u9fa5]+$/i;
				            return reg.test(value);
				        },
				        message: "姓名请输入中文"
				    },
				    pwd:{
				    	validator: function(value, param) {
				            var reg =  /^.{6,}$/;
				            return reg.test(value);
				        },
				        message: "密码长度不满足6位"
				    },
				    //身份证号验证
				   cradsumAge:{
			            validator: function(value, param) {
						var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;			
						    return reg.test(value);
			            },
	                    message: "请输入正确的身份证号"
	                },
				    ///手机号判断
				    telcheck:{
				    	validator:function(value,param){
				    		 var regTel=/(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
				    		 return regTel.test(value);
				    	},
				    	message:"请输入正确的手机号"
				    },
				    //地址验证
				    addresscheck:{
				    	validator: function(value, param) {
				            var addressreg = /^[\u4e00-\u9fa5]+$/i;
				            return addressreg.test(value);
				        },
				        message: "请输入正确的地址"
				    }
				    
		   });//自定义验证
</script>
<div class="maxbox easyui-panel"  data-options="fit:true" style="border:1px solid #F3F3F4;">

      <fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:1050px;margin:0 auto;margin-top:20px;">
 	    <legend style="font-size:17px;">用户管理</legend>
       <div style="margin-top:7px;margin-left: 28px;">
          <input id="ss"  class="search-user easyui-searchbox" style="width:200px" 
          data-options="prompt:'请输入用户名字'"></input> 
		</div>
      <br>	
     <div style="margin-left: 30px">

         <table id="user" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:false,width:1043,height:320">
		   
	 	 </table>
	 </div>
	 <div class="button" style="float: right;margin-bottom:5px;margin-top: 18px;    margin-right: 7px;">
    	<a  class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="userinsert(this)">添加</a> 
        <!-- <a id="btn" class="easyui-linkbutton" data-options="iconCls:'icon-add'">删除</a> --> 
       
     </div>	 
    
		
 	  </fieldset>

<!-- 用户新增 -->
  <div id="quanxian-adduser" align="center" class="easyui-window" title="用户新增" style="width:550px;height:380px;"  data-options="iconCls:'icon-save',modal:true,
  resizable:false, minimizable:false,maximizable:false,collapsible:false,closed:true,zIndex:300,singleSelect:true">   	          
       <p style="font-size:18px;">添加用户</p>
          
      <form id="ff" method="post" > 
	    <div style="font-size: 17px;float: left;margin-left: 25px;margin-right: 30px;margin-bottom: 20px;">   
	      <span style="margin-right:22px;margin-left: 14px;"> 姓名</span>
	      <input  class="easyui-textbox" required="true" id="tjdxm" style="width:120px;height:27px;" name="name" placeholder="请输入姓名"
	      data-options="validType:['chinese']" /> 				          
	    </div>
	    
	    <div style="float: left;font-size: 17px;margin-bottom: 20px;margin-left: 66px;">   				      
	        <span style="margin-right:19px;margin-left: 3px;">密码</span>
	        <input  class="easyui-textbox" required="true" id="tjdmm" style="width:120px;height:27px;"  name="password" placeholder="请输入密码"
	        data-options="validType:['pwd']" />				          
	    </div>
	     
	   <div style="margin-top: 6px;font-size: 17px;float: left;margin-left: 25px;margin-right: 32px;margin-bottom: 20px;">  
	        <span style="margin-right:22px;margin-left: 14px;">科室</span>
	        <select id="keshiID" name="keshi" class="role easyui-combobox" style="width:120px;height:27px;">   
			  </select> 
	   </div> 
	    
	   <div style="margin-top: 6px;float: left;font-size: 17px;    margin-bottom: 20px;">
	       <span style="margin-right:19px;margin-left: 66px;">角色</span>
	       <select id="roleIDs" name="roleId" class="role easyui-combobox" style="width:120px;height:27px;">   
		   </select>   
	    </div> 
	   
	  <div style="margin-top: 6px;font-size: 17px;float: left;margin-left: 25px;margin-right: 31px;margin-bottom: 20px;">  
	       <span style="margin-right:22px;margin-left:14px;">性别</span>
	       <!-- <input  class="easyui-textbox" required="true" style="width:120px;height:27px;"name="sex" placeholder="请输入性别"/>    -->
	      <select class="easyui-combobox"  data-options="panelHeight:'auto' " required="true" style="width:120px;height:27px;"name="sex">   
		     <option>男</option>
		     <option>女</option>
		  </select> 
	 </div> 
	    				    
		   <div style="float: left;font-size: 17px;margin-top: 6px;margin-bottom: 20px;">
		 	 <span style="margin-right:19px;margin-left: 66px;">地址</span>
		 	 <input  class="easyui-textbox" id="tjddd" required="true"style="width:120px;height:27px;" name="Address" placeholder="请输入家庭地址"
		 	data-options="validType:['addresscheck']" />   
		 </div> 
		      
		  <div style="float: left;font-size: 17px;margin-top: 6px;margin-bottom: 10px;">
		        <span style="margin-right:7px;margin-left: 37px;">身份证</span>
		        <input  class="easyui-textbox" id="tjdsfz" required="true" style="width:120px;height:27px;"name="card"  placeholder="请输入身份证"
		        data-options="validType:['cradsumAge']" />   
		 </div>
		    
		<div style="font-size: 17px;float: left;margin-left: 27px;margin-top: 6px;"> 
		 	 <span style="margin-right:7px;margin-left: 66px;">手机号</span>
		 	 <input  class="easyui-textbox" id="tjdsjh" required="true"style="width:120px;height:27px;" name="tel" placeholder="请输入手机号"
		 	 data-options="validType:['telcheck']"/>   
		 </div>       
		      
		      
	     <div style="float: left;margin-top:44px;margin-left: 12px;">	
	      <a id="userinserts"  class="easyui-linkbutton" data-options="iconCls:'icon-ok'">新增</a>  
	     
	      </div> 
		</form>
</div> 	

		
	<div class="easyui-window" id="updatepwda" title="修改密码" style="width:350px;height:180px;" data-options="iconCls:'icon-save',modal:true,
	   resizable:false, minimizable:false,maximizable:false,shadow:true,collapsible:false,closed:true,zIndex:300,singleSelect:true">
			<div class="easyui-layout" data-options="fit:true" style="text-align: center;">
				    <input id="userid" class="easyui-textbox" type="text"  data-options="revealed:true,showEye:false,height:28"/>   
			       			    
				    <div style="margin-top:30px;margin-bottom: 30px">   				      
				        <label for=""  style="font-size:16px;">新密码：</label>   
				        <input class="easyui-textbox" type="text" id="updatpwdq"   data-options="prompt:'请输入新密码',
				        revealed:true,showEye:false,height:28"/>   
				    </div>  
				     <a id="queding"  class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确定</a>
				
		    </div>
	</div>
	
	<!-- 用户详情 -->
	  <div class="easyui-window" id="userxiangqing1" title="用户详情" style="width:736px;height:502px;" data-options="modal:true,
	   resizable:false, minimizable:false,maximizable:false,shadow:true,collapsible:false,closed:true,zIndex:300,singleSelect:true">
		 <table id="userxiangqing" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:false,width:722">
		   
	 	 </table>
	 </div>
	
	<!-- 用户角色 -->
	  <div class="easyui-window" id="select-role1" title="用户角色" style="width:736px;height:502px;" data-options="modal:true,
	   resizable:false, minimizable:false,maximizable:false,shadow:true,collapsible:false,closed:true,zIndex:300,singleSelect:true">
		 <table id="select-role" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:false,width:722">
		   
	 	 </table>
	 </div>
	 
	 <!-- 用户权限 -->
	  <div class="easyui-window" id="select-function1" title="用户权限" style="width:596px;height:502px;" data-options="modal:true,
	   resizable:false, minimizable:false,maximizable:false,shadow:true,collapsible:false,closed:true,zIndex:300,singleSelect:true">
		 <div style="display: none;">
							  <input  id="useridss"  class="easyui-textbox" type="text"  data-options="revealed:true,showEye:false,height:28"/>  
						</div>	
	 					<div data-options="region:'center'">
							<ul class="easyui-tree" id="userfunction"></ul>
						</div>

	 </div>
	 
	 <!-- 添加角色 --> 	
	<div class="easyui-window" id="tianjiarole" title="添加角色" style="width:350px;height:500px;" data-options="iconCls:'icon-save',modal:true,
	   resizable:false, minimizable:false,maximizable:false,shadow:true,collapsible:false,closed:true,zIndex:300,singleSelect:true">
			<div class="easyui-layout" data-options="fit:true" style="text-align: center;">					
						<div style="display: none;">
							  <input  id="userids"  class="easyui-textbox" type="text"  data-options="revealed:true,showEye:false,height:28"/>  
						</div>	  
						<div data-options="region:'south',border:false" style="text-align:right">
						   <input id="queren" type="button" value="确定">	
						</div>     			    
						<div data-options="region:'center'">
							<ul class="easyui-tree" id="grant-tree1"></ul>
						</div>							
		    </div>
	</div>
		
	 
			  
</div>
</body>
</html>